package com.yyfax.ph.personal.company;

import android.text.TextUtils;
import com.yyfax.common.CLog;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.IView;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterFactory;
import com.yyfax.infrastructure.statistics.StatService;
import com.yyfax.ph.StatIDSet;
import com.yyfax.ph.base.YJPH;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * 绑定公司和解绑公司都用这个presenter ，companyName为""时表示解绑公司
 */
public class BindCompanyPresenter extends Presenter<BindCompanyModel, BindCompanyPresenter.View> {
	private static final String TAG = BindCompanyPresenter.class.getSimpleName();
	private View mView;

	public static class Generator implements PresenterFactory<BindCompanyPresenter> {
		@Override
		public BindCompanyPresenter create() {
			return new BindCompanyPresenter();
		}
	}

	public interface View extends IView {
		void bindCompanySuccess(String tips);

		void bindCompanyFailure(String tips);

		void unbindCompanySuccess(String tips);
	}


	@Override
	public void attach(View view) {
		mView = view;
		mModel = new BindCompanyModel();
		EventAgent.register(this);
	}

	@Override
	public void detach() {
		mView = null;
		mModel = null;
		EventAgent.unregister(this);
	}

	/**
	 * 绑定公司
	 */
	public void bindCompany(String companyName) {
		CLog.i(TAG, "bindCompany");
		execute(new BindCompanyModel.Company(companyName), FUNCTIONAL_CODE, true, companyName, null);

	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void companyResult(BindCompanyModel.CompanyResult result) {
		StatService.onEventFirstKeyAttribute(YJPH.getInstance(), StatIDSet.EVENT_ID_E033, result.getStatus());
		String tips = result.getMsg();
		CLog.i(TAG, "companyResult");
		if (isResponseSuccess(result)) {
			if (TextUtils.isEmpty((String) result.extraParam)) {//根据公司名称是否为空来判断是绑定请求还是解绑请求
				mView.unbindCompanySuccess("");
			} else {
				mView.bindCompanySuccess(tips);
			}
		} else {
			mView.bindCompanyFailure(TextUtils.isEmpty(tips) ? "绑定失败" : tips);
		}
	}
}
